package com.gf.exam.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.gf.base.type.Activity;
import com.gf.base.type.ExamType;
import com.gf.base.type.PlayMode;
import com.gf.common.util.DateFormater;
import com.gf.exam.model.Exam;
import com.gf.exam.model.Timer;
/**
 * 
 * @author zhuguifeng guifeng.net@gmail.com
 * @version 创建时间：2012-12-1 下午4:55:30
 */
public class ExamMapper implements RowMapper<Exam> {

	private static final ExamMapper mapper = new ExamMapper();

	private static final String COLUME_ID = "id";
	private static final String COLUME_USER_ID = "userId";
	private static final String COLUME_USERNAME = "userName";
	private static final String COLUME_TITLE = "title";
	private static final String COLUME_INTRODUCTION = "introduction";
	private static final String COLUME_CREATETIME = "createtime";
	private static final String COLUME_UPDATETIME = "updatetime";
	private static final String COLUME_CELLNO = "cellNo";
	private static final String COLUME_SUBMITNO = "submitNo";
	
	private static final String COLUME_ACTIVE = "active";
	private static final String COLUME_REPEAT_FROM = "repeatFrom";
	private static final String COLUME_REPEAT_TO = "repeatTo";
	private static final String COLUME_PLAY_MODE = "playMode";
	private static final String COLUME_REPEAT_TIME = "repeatTime";
	
	private static final String COLUME_VALIDNO = "validNo";
	private static final String COLUME_TIMER_H = "timerH";
	private static final String COLUME_TIMER_M = "timerM";
	private static final String COLUME_TIMER_S = "timerS";
	private static final String COLUME_EXAMTYPE = "examType";
	private static final String COLUME_ACTIVITY = "activity";

	public static ExamMapper getMapper() {
		return mapper;
	}

	@Override
	public Exam mapRow(ResultSet set, int row) throws SQLException {
		Exam exam = new Exam();

		exam.setId(set.getLong(COLUME_ID));
		exam.setUserId(set.getLong(COLUME_USER_ID));
		exam.setUserName(set.getString(COLUME_USERNAME));
		exam.setTitle(set.getString(COLUME_TITLE));
		exam.setIntroduction(set.getString(COLUME_INTRODUCTION));
		exam.setCreatetime(set.getTimestamp(COLUME_CREATETIME));
		exam.setFormatCreatetime(DateFormater.changeToString(set.getTimestamp(COLUME_CREATETIME), "yyyy-MM-dd HH:mm"));
		exam.setFormatUpdatetime(DateFormater.changeToString(set.getTimestamp(COLUME_UPDATETIME), "yyyy-MM-dd HH:mm"));
		exam.setUpdatetime(set.getTimestamp(COLUME_UPDATETIME));
		exam.setCellNo(set.getInt(COLUME_CELLNO));
		exam.setSubmitNo(set.getLong(COLUME_SUBMITNO));
		exam.setValidNo(set.getLong(COLUME_VALIDNO));
		exam.setActive(set.getBoolean(COLUME_ACTIVE));
		exam.setRepeatFrom(set.getInt(COLUME_REPEAT_FROM));
		exam.setRepeatTo(set.getInt(COLUME_REPEAT_TO));
		exam.setPlayMode(PlayMode.valueOf(set.getString(COLUME_PLAY_MODE)));
		exam.setRepeatTime(set.getInt(COLUME_REPEAT_TIME));
		int h = set.getInt(COLUME_TIMER_H);
		int m = set.getInt(COLUME_TIMER_M);
		int s = set.getInt(COLUME_TIMER_S);
		exam.setTimer(new Timer(h, m, s));
		exam.setExamType(ExamType.valueOf(set.getString(COLUME_EXAMTYPE)));
		exam.setActivity(Activity.valueOf(set.getString(COLUME_ACTIVITY)));

		return exam;
	}

}
